/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the lkyh.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.lkyh.mes.codegen.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkyh.mes.codegen.entity.GenPageInfo;
import com.lkyh.mes.codegen.mapper.GenPageInfoMapper;
import com.lkyh.mes.codegen.service.GenPageInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 表单管理
 *
 * @author lengleng
 * @date 2019-08-12 15:55:35
 */
@Service
@AllArgsConstructor
public class GenPageInfoServiceImpl extends ServiceImpl<GenPageInfoMapper, GenPageInfo> implements GenPageInfoService {


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void up(Integer id) {
		GenPageInfo genPageInfo = this.getById(id);
		int newOrder = genPageInfo.getOrder() - 1;
		List<GenPageInfo> list = this.list(Wrappers.query(new GenPageInfo().setTableId(genPageInfo.getTableId())));
		list.forEach(i -> {
			if (NumberUtil.compare(i.getOrder(), genPageInfo.getOrder()) == -1 &&
					NumberUtil.compare(i.getOrder(), newOrder) == 0) {
				i.setOrder(i.getOrder() + 1);
			}
			if (id.equals(i.getId())) {
				i.setOrder(newOrder);
			}
		});
		this.updateBatchById(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void down(Integer id) {
		GenPageInfo genPageInfo = this.getById(id);
		int newOrder = genPageInfo.getOrder() + 1;
		List<GenPageInfo> list = this.list(Wrappers.query(new GenPageInfo().setTableId(genPageInfo.getTableId())));
		list.forEach(i -> {
			if (NumberUtil.compare(i.getOrder(), genPageInfo.getOrder()) == 1 &&
					NumberUtil.compare(i.getOrder(), newOrder) == 0) {
				i.setOrder(i.getOrder() - 1);
			}
			if (id.equals(i.getId())) {
				i.setOrder(newOrder);
			}
		});
		this.updateBatchById(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void orderChange(Integer id, Integer newOrder) {
		GenPageInfo genPageInfo = this.getById(id);
		List<GenPageInfo> list = this.list(Wrappers.query(new GenPageInfo().setTableId(genPageInfo.getTableId())));
		Integer oldOrder = genPageInfo.getOrder();
		list.forEach(i -> {
			if (newOrder > oldOrder) {
				if (i.getOrder() > oldOrder && i.getOrder() <= newOrder) {
					i.setOrder(i.getOrder() - 1);
				}
			} else {
				if (i.getOrder() < oldOrder && i.getOrder() >= newOrder) {
					i.setOrder(i.getOrder() + 1);
				}
			}
			if (id.equals(i.getId())) {
				i.setOrder(newOrder);
			}
		});
		this.updateBatchById(list);
	}


}
